<?php
/**
 * 调查管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-09
 */
namespace Admin\Controller; 

class ExamenController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db_exam      =D("Examen");
        $this->db_statistics=D("Statistics");
        $this->db_feedback  =D("Feedback");
        $this->db_stat_rop  =D("StatisticsRoportion");
        $this->db_an_list   =D("AnswerList");
    } 
    
    /*
     * ajax 指定调查项
     * 
     * @return  #
     */
    public function ajaxCheck(){
        if(I("request.id")){
            $map1['is_examen']  =0;
            $map1['id']         =array("gt",0);
            $map['id']          =I("request.id");
            $map['is_examen']   =1;
            if($this->db_exam->where(array("is_examen"=>1))->find()){
                if($this->db_exam->save($map1)){                    
                    if($this->db_exam->save($map)){
                        $this->ajaxReturn(TRUE,'JSON');
                    }else{
                        $this->ajaxReturn(FALSE,'JSON');
                    }
                }else{
                    $this->ajaxReturn(FALSE,'JSON');
                }
            }else{
                if($this->db_exam->save($map)){
                    $this->ajaxReturn(TRUE,'JSON');
                }else{
                    $this->ajaxReturn(FALSE,'JSON');
                }
            }            
        }else{
            $this->ajaxReturn(FALSE,'JSON');
        }
    }
    
    /*
     * show前置操作
     * 
     * @return #
     */
    public function _before_show(){
        if(I("request.is_sign",'','code')==1){
            if($this->is_answer()){
                $this->error("请先删除备选答案");
            }
            if($this->is_feedback(I("request.id",'','code'))==1){
                $this->error("参数错误");
            }else if($this->is_feedback(I("request.id",'','code'))==2){
                $this->error("该问题已反馈，不允许修改");
            }
        }else if(I("request.is_sign",'','code')==2){
            if($this->is_feedback('',I("request.id",'','code'))==1){
                $this->error("参数错误");
            }else if($this->is_feedback('',I("request.id",'','code'))==2){
                $this->error("该答案已提交反馈，不允许修改");
            }
        }
        $qList  =$this->db_exam->where(array("status"=>1))->order("add_time desc")->limit(0,5)->select();
        $this->assign("qList",$qList);
    }
    
    /*
     * 备选答案列表
     * 
     * @return #
     */
    public function answerList(){        
        if(I("request.question")){
            $_REQUEST['exa_id'] =A("Common")->getIds(I("request.question"),"question","Examen");
        }
        $oerder =array(
            "exa_id"    =>desc,
            'sort_'     =>asc,
            'add_time'  =>asc
        );
        A("Common")->index("AnswerList","exa_id");
    }
    
    /*
     * 反馈列表
     * 
     * @return #
     */
    public function feedback(){
        if(I("request.question")){
            $_REQUEST['exa_id']     =A("Common")->getIds(I("request.question"),"question","Examen");
        }
        if(I("request.answer")){
            $_REQUEST['ans_id']     =A("Common")->getIds(I("request.answer"),"answer","AnswerList");
        }
        if(I("request.assign_user")){
            $_REQUEST['assign_id']  =A("Common")->getIds(I("request.assign_user"),"name","User");
        }
        A("Common")->index("Feedback",'add_time',false);
    }
    
    /*
     * 反馈统计列表
     * 
     * @return #
     */
    public function statistics(){
        if(I("request.question")){
            $_REQUEST['exa_id'] =A("Common")->getIds(I("request.question"),"question","Examen");
        }
        A("Common")->index("Statistics");
    }
    
    /*
     * 统计详情
     * 
     * @return #
     */
    public function show_(){
        if(!I("request.exa_id",'','code')){
            $this->error("参数错误");
        }
        $ansList=$this->db_stat_rop->where(array("exa_id"=>I("request.exa_id",'','code')))->select();
        $this->assign("vo",$ansList);
        $this->display();
    }
    
    /*
     * 个人投票次数统计
     * 
     * @return #
     */
    public function statisticVote(){
        if(I("request.user_id")){
            $_REQUEST['user_id']  =$this->getIds(I("request.user_id"),"name","User");
        }
        $this->index("StatisticVote",'add_time');
    }
    
    /*
     * 删除前置操作
     * 
     * @return #
     */
    public function _before_del(){
        if(I("request.sign",'','code')==1){
            if($this->is_answer()){
                $this->error("请先删除备选答案");
            }
            if($this->is_feedback(I("request.id",'','code'))==1){
                $this->error("参数错误");
            }else if($this->is_feedback(I("request.id",'','code'))==2){
                $this->error("该问题已反馈，不允许删除");
            }
        }else if(I("request.sign",'','code')==2){
            if($this->is_feedback('',I("request.id",'','code'))==1){
                $this->error("参数错误");
            }else if($this->is_feedback('',I("request.id",'','code'))==2){
                $this->error("该答案已提交反馈，不允许删除");
            }
        }
    }
    
    
    /*
     * 判断备选答案是否存在
     * 
     * @return #
     */
    public function is_answer(){
        $map['exa_id']  =I("request.id",'','code');
        if(is_array(I("request.id"))){
            $map['exa_id']  =array("IN",implode(',',I("request.id")));
        }
        $map['status']  =1;
        $is_anList      =$this->db_an_list->where($map)->find();
        if($is_anList){
            return TRUE;
        }
    }
    
    /*
     * 判断反馈是否存在
     * 
     * @return #
     */
    public function  is_feedback($exa_id='',$ans_id=''){
        if($exa_id=='' && $ans_id==''){
            return 1;
        }else if($exa_id && $ans_id==''){
            $map['exa_id']      =$exa_id;
            if(is_array($exa_id)){
                $map['exa_id']  =array("IN",implode(',',$exa_id));
            }
        }else if($exa_id=='' && $ans_id){
            $map['ans_id']      =$ans_id;
            if(is_array($ans_id)){
                $map['ans_id']  =array("IN",implode(",",$ans_id));
            }
        }else if($exa_id && $ans_id){
            $map['exa_id']  =$exa_id;
            $map['ans_id']  =$ans_id;
            if(is_array($exa_id)){
                $map['exa_id']  =array("IN",implode(',',$exa_id));
            }
            if(is_array($ans_id)){
                $map['ans_id']  =array("IN",implode(",",$ans_id));
            }
        }
        $map['status']      =1;
        if($this->db_feedback->where($map)->find()){
            return 2;
        }
    }
}
